/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.config;


import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceHttpClientImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaRedissonConfigImpl;
import cn.hutool.core.util.StrUtil;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.service.EntPayService;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.EntPayServiceImpl;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.yami.shop.common.bean.WxApp;
import com.yami.shop.common.bean.WxMiniApp;
import com.yami.shop.common.bean.WxMp;
import com.yami.shop.common.bean.WxPay;
import com.yami.shop.common.enums.PayType;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceHttpClientImpl;
import me.chanjar.weixin.mp.config.impl.WxMpRedissonConfigImpl;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 通过微信配置获取微信的支付信息，登陆信息等
 * @author LGH
 */
@Component
@AllArgsConstructor
public class WxConfig {

    private final ShopConfig shopConfig;

    private final RedissonClient redissonClient;


    private static final String MA_CONFIG_KEY_PREFIX = "mall4j:ma_config_key_prefix";

    private static final String MP_CONFIG_KEY_PREFIX = "mall4j:mp_config_key_prefix";

    /**
     * 根据支付方式，获取微信支付信息
     * @param payType
     * @return
     */
    public WxPayService getWxPayService(PayType payType) {

        String appid;
        // 小程序支付
        if (Objects.equals(payType, PayType.WECHATPAY)) {
            WxMiniApp wxMiniApp = shopConfig.getWxMiniApp();
            appid = wxMiniApp.getAppId();
        } else if (Objects.equals(payType, PayType.WECHATPAY_APP)) {
            WxApp wxApp = shopConfig.getWxApp();
            appid = wxApp.getAppId();
        } else {
            WxMp wxMp = shopConfig.getWxMp();
            appid = wxMp.getAppId();
        }

        WxPayConfig payConfig = getWxPay(appid);
        payConfig.setSignType(WxPayConstants.SignType.MD5);

        WxPayService wxPayService = new WxPayServiceImpl();

        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

    public WxMaService getWxMaService() {
        WxMiniApp wxMiniApp = shopConfig.getWxMiniApp();
        // access token用redis缓存
        WxMaRedissonConfigImpl maRedissonConfig = new WxMaRedissonConfigImpl(redissonClient, MA_CONFIG_KEY_PREFIX);
        maRedissonConfig.setAppid(wxMiniApp.getAppId());
        maRedissonConfig.setSecret(wxMiniApp.getSecret());

        WxMaServiceHttpClientImpl wxMaServiceHttpClient = new WxMaServiceHttpClientImpl();
        wxMaServiceHttpClient.setWxMaConfig(maRedissonConfig);
        return wxMaServiceHttpClient;
    }


    public WxMpService getWxMpService() {
        WxMp wxMp = shopConfig.getWxMp();
        // access token用redis缓存
        WxMpRedissonConfigImpl wxMpRedisConfig = new WxMpRedissonConfigImpl(redissonClient, MP_CONFIG_KEY_PREFIX);
        wxMpRedisConfig.setAppId(wxMp.getAppId());
        wxMpRedisConfig.setSecret(wxMp.getSecret());

        WxMpServiceHttpClientImpl wxMpServiceHttpClient = new WxMpServiceHttpClientImpl();
        wxMpServiceHttpClient.setWxMpConfigStorage(wxMpRedisConfig);
        return wxMpServiceHttpClient;
    }


    public EntPayService getEntPayService() {
        String appid;
        WxMiniApp wxMiniApp = shopConfig.getWxMiniApp();
        // 小程序支付
        if (wxMiniApp != null && StrUtil.isNotBlank(wxMiniApp.getAppId())) {

            appid = wxMiniApp.getAppId();
        } else {
            WxMp wxMp = shopConfig.getWxMp();
            appid = wxMp.getAppId();
        }

        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(getWxPay(appid));
        return new EntPayServiceImpl(wxPayService);
    }

    private WxPayConfig getWxPay(String appid) {
        WxPay wxPay = shopConfig.getWxPay();
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(appid);
        payConfig.setMchId(wxPay.getMchId());
        payConfig.setMchKey(wxPay.getMchKey());
        payConfig.setKeyPath(wxPay.getKeyPath());
        return payConfig;
    }
}
